package com.yjck.ijpay.helper;


import com.yjck.ijpay.utils.ConfigUtil;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * @author jiucai created at 2018-06-18 11:49
 * @version 1.0.0
 **/
public class ConfigHelper {

    protected static Logger logger = LoggerFactory.getLogger(ConfigHelper.class);
    private static Configuration cfg;

    public static Configuration getConfig() {
        if (null == cfg) {
//            ConfigUtil.addConfig("env");
            cfg = ConfigUtil.addConfig("config");
        }

        return cfg;
    }


    public static String getContent(String classPath) {
        String result = "";
        InputStream ins = null;
        try {

            ins = ConfigHelper.class.getClassLoader().getResourceAsStream(classPath);

            List<String> strList = IOUtils.readLines(ins, "UTF-8");
            result = StringUtils.join(strList, "\n");


        } catch (Exception e) {
            result = "";
            logger.error("read file {} failed", classPath, e);
        } finally {
            if (null != ins) {
                try {
                    ins.close();
                } catch (IOException e) {
                    logger.error("failed to close io stream", e);
                }
            }
        }

        return result;


    }
}
